package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.WxAddressSaveBo;
import com.cskaoyan.bean.pojo.MallAddress;
import com.cskaoyan.bean.pojo.MallAddressExample;
import com.cskaoyan.bean.pojo.MallRegion;
import com.cskaoyan.bean.vo.WxAddressDetailVo;
import com.cskaoyan.bean.vo.WxAddressListVo;
import com.cskaoyan.mapper.MallAddressMapper;
import com.cskaoyan.mapper.MallRegionMapper;
import com.cskaoyan.service.WxAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 小程序: 收货地址
 * @Author: zoey-zhu
 * @Date: 16:19 2021/7/12
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MallAddressMapper mallAddressMapper;

    @Autowired
    MallRegionMapper mallRegionMapper;

    /**
     * @Author: zoey-zhu
     * @description: 小程序收货地址，获取对应的用户的全部，并查询地区地址：省、市、区
     * @Date: 16:16 2021/7/12
     * @url: http://182.92.235.201:8083/wx/address/list
     */
    public List<WxAddressListVo> list(Integer userId){

        MallAddressExample mallAddressExample = new MallAddressExample();
        MallAddressExample.Criteria criteria = mallAddressExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);

        List<MallAddress> mallAddressList = mallAddressMapper.selectByExample(mallAddressExample);
        List<WxAddressListVo> wxAddressVoList = new ArrayList<>();

        for (MallAddress mallAddress : mallAddressList) {
            MallRegion mallRegion1 = mallRegionMapper.selectByPrimaryKey(mallAddress.getProvinceId());
            MallRegion mallRegion2 = mallRegionMapper.selectByPrimaryKey(mallAddress.getCityId());
            MallRegion mallRegion3 = mallRegionMapper.selectByPrimaryKey(mallAddress.getAreaId());
            String detailedAddress = mallRegion1.getName() + mallRegion2.getName() + mallRegion3.getName() + " " + mallAddress.getAddress();

            WxAddressListVo wxAddress = new WxAddressListVo(mallAddress.getIsDefault(), detailedAddress, mallAddress.getName(), mallAddress.getMobile(), mallAddress.getId());
            wxAddressVoList.add(wxAddress);
        }

        return wxAddressVoList;
    }

    /**
     * @Author: zoey-zhu
     * @description: 小程序收货地址，获取对应的用户的地址(通过地址id)
     * @Date: 17:08 2021/7/12
     * @url: http://182.92.235.201:8083/wx/address/detail?id=5
     */
    @Override
    public WxAddressDetailVo detailById(Integer id) {

        MallAddress mallAddress = mallAddressMapper.selectByPrimaryKey(id);

        MallRegion province = mallRegionMapper.selectByPrimaryKey(mallAddress.getProvinceId());
        MallRegion city = mallRegionMapper.selectByPrimaryKey(mallAddress.getCityId());
        MallRegion area = mallRegionMapper.selectByPrimaryKey(mallAddress.getAreaId());

        WxAddressDetailVo wxAddressDetailVo = new WxAddressDetailVo(mallAddress.getIsDefault(), mallAddress.getAreaId(),
                mallAddress.getAddress(), city.getName(), area.getName(), mallAddress.getName(),mallAddress.getMobile(),
                mallAddress.getId(), mallAddress.getCityId(), province.getName(), mallAddress.getProvinceId());

        return wxAddressDetailVo;
    }

    /**
     * @Author: zoey-zhu
     * @description: 小程序收货地址，对应的用户的地址，更新或者新增
     *              （根据id是否为0，为0insert，否则update），
     *              注意所以地址只能有一个默认地址：isDefault为true或者为1
     * @Date: 17:08 2021/7/12
     * @url: http://182.92.235.201:8083/wx/address/save
     */
    @Override
    public Integer saveUpdateOrAdd(WxAddressSaveBo saveBo, Integer userId) {

        //判断当前的更新或新增的地址是否设置为默认地址，
        // 若为默认地址，就将其他所有当前用户的所有其他地址的默认字段设为0
        if (saveBo.getIsDefault()){
            int updateRows = mallAddressMapper.saveUpdateIsDefault(userId);
        }

        //若id=0，为新增，获得插入后生成的id，并返回插入后获得的id
        if (saveBo.getId() == 0){

//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date date = new Date();
//            String format = simpleDateFormat.format(date);

            Date addTime = new Date();
            Date updateTime = new Date();
            boolean deleted = false;

            MallAddress mallAddress = new MallAddress(null, saveBo.getName(), userId,
                    saveBo.getProvinceId(), saveBo.getCityId(), saveBo.getAreaId(), saveBo.getAddress(),
                    saveBo.getMobile(), saveBo.getIsDefault(), addTime, updateTime, deleted);

            int insert = mallAddressMapper.insertReturnId(mallAddress);

            return mallAddress.getId();
        }

        //若id != 0，为更新，更新所以不为null的字段，并返回id
        Date updateTime = new Date();
        MallAddress mallAddress = new MallAddress(saveBo.getId(), saveBo.getName(), userId,
                saveBo.getProvinceId(), saveBo.getCityId(), saveBo.getAreaId(), saveBo.getAddress(),
                saveBo.getMobile(), saveBo.getIsDefault(), null, updateTime, null);

        int insert = mallAddressMapper.updateByPrimaryKeySelective(mallAddress);

        return saveBo.getId();
    }

    /**
     * @Author: zoey-zhu
     * @description: 小程序收货地址，对应的用户的某个地址（根据地址id删除）
     * @Date: 18:59 2021/7/12
     * @url: http://182.92.235.201:8083/wx/address/delete
     */
    @Override
    public int deleteById(Integer id) {

        MallAddress mallAddress = new MallAddress();
        mallAddress.setId(id);
        mallAddress.setDeleted(true);

        int delete = mallAddressMapper.updateByPrimaryKeySelective(mallAddress);
        return delete;
    }

}
